# Maintainer: Alexander F. Rødseth <xyproto@archlinux.org>
# Contributor: Lukas Fleischer <lfleischer@archlinux.org>
# Contributor: Allan McRae <allan@archlinux.org>
# Contributor: bekks <eduard.warkentin@gmail.com>
# Contributor: Daniel J Griffiths <ghost1227@archlinux.us>
# Contributor: Wang Haoyu

pkgname=upx
pkgver=4.2.1
pkgrel=2
pkgdesc='Extendable, high-performance executable packer for several executable formats'
arch=(loong64 x86_64)
url='https://github.com/upx/upx'
license=(custom GPL2)
makedepends=(cmake git perl)
# https://github.com/upx/upx/tree/v4.2.1/vendor
# b='v4.2.1'; for x in doctest lzma-sdk ucl valgrind zlib; do echo "git+\$url-vendor-$x#commit=$(git ls-remote https://github.com/upx/upx-vendor-$x $b-upx-vendor | cut -b-40)"; done
source=(
  git+$url-vendor-doctest#commit=15e49a5fb7e49dd8f590d3666534b209e7212c6c
  git+$url-vendor-lzma-sdk#commit=f0b8d236c1bdf66532a198c983469da5c05593cf
  git+$url-vendor-ucl#commit=2c68ae51994892b9080328617a84296ee55f0c80
  git+$url-vendor-valgrind#commit=f86c669e0112e5cf88027264eb6be8c4921a512a
  git+$url-vendor-zlib#commit=29fceaffe38ef80e404bfa92a47b192b6e887d15
  "git+$url#commit=d14fa5f8a410da566f61bb1ca49cb2157b54ed93") # tag: v4.2.1
source+=(0001-just-for-la64-build.patch)
b2sums=('SKIP'
        'SKIP'
        'SKIP'
        'SKIP'
        'SKIP'
        'SKIP')
b2sums+=('0ea1b7a64155d135bfd8872969e1003b72d4b098512f1bc89d31244e02ffc901cc8c5bd87eb63a529aadf161386cd857cc6271a8ea018ba3915616c3c2fe5c72')

prepare() {
  cd $pkgname
  # Set up symbolic links for the vendor repos.
  for x in doctest lzma-sdk ucl valgrind zlib; do
    rm -frv vendor/$x && ln -s "$srcdir/upx-vendor-$x" vendor/$x
  done
  patch -p1 -i $srcdir/0001-just-for-la64-build.patch
}

build() {
  make -C $pkgname \
    CHECK_WHITESPACE=/bin/true \
    CXXFLAGS_WERROR="" \
    UPX_CMAKE_CONFIG_FLAGS='-D UPX_CONFIG_DISABLE_GITREV=1 -D UPX_CONFIG_DISABLE_SANITIZE=1 -D UPX_CONFIG_DISABLE_WERROR=1' \
    UPX_LZMA_VERSION=0x465 \
    UPX_LZMADIR="$srcdir"
}

check() {
  # Check that the version has the proper version number and not something like "git-877911+".
  $pkgname/build/release/upx --help | grep -q "^UPX $pkgver" || exit 1
  # Run tests
  make -C $pkgname/build/release test
}

package() {
  cd $pkgname
  install -Dm755 build/release/upx "$pkgdir/usr/bin/upx"
  install -Dm644 doc/upx.1 "$pkgdir/usr/share/man/man1/upx.1"
  install -Dm644 LICENSE "$pkgdir/usr/share/licenses/$pkgname/LICENSE"
}
